package com.cabbagetofu.crowd.controller;


import com.atguigu.crowd.until.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class RedisProviderController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    // 设置redis的key和value
    @RequestMapping("/set/redis/key/value/remote")
    ResultEntity<String> setRedisKeyValueRemote(@RequestParam("key") String key,
                                                @RequestParam("value") String value){

        try {

            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            return ResultEntity.successWithoutData();

        } catch (Exception e) {
            // 如果失败则返回失败信息
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());

        }

    }


    // 设置redis的key和value, 包含超时时间
    @RequestMapping("/set/redis/key/value/remote/timeout")
    ResultEntity<String> setRedisKeyValueRemoteWithTimeout(@RequestParam("key") String key,
                                                           @RequestParam("value") String value,
                                                           @RequestParam("time") long time,
                                                           @RequestParam("timeUnit") TimeUnit timeUnit){

        try {
            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            operations.set(key, value, time, timeUnit);
            return ResultEntity.successWithoutData();

        } catch (Exception e) {
            // 如果失败则返回失败信息
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());

        }

    }

    // 根据key获取对应值
    @RequestMapping("/get/redis/string/value/by/key")
    ResultEntity<String> getRedisStringValueByKey(@RequestParam("key") String key){

        try {

            ValueOperations<String, String> operations = redisTemplate.opsForValue();
            String value = operations.get(key);

            return ResultEntity.successWithData(value);

        } catch (Exception e) {
            // 如果失败则返回失败信息
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());

        }
    }

    // 根据key删除对应值
    @RequestMapping("/remove/redis/string/value/by/key")
    ResultEntity<String> removeRedisStringValueByKey(@RequestParam("key") String key){

        try {

            redisTemplate.delete(key);
            return ResultEntity.successWithoutData();

        } catch (Exception e) {
            // 如果失败则返回失败信息
            e.printStackTrace();
            return ResultEntity.failed(e.getMessage());

        }

    }

}
